package org.xserver.component.core.websocket;

import static org.xserver.component.core.interfaces.InterfaceContext.DEFAULT_INTERFACE_SPLIT;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.xserver.common.util.ReflectionUtil;
import org.xserver.component.annotation.Path;
import org.xserver.component.annotation.WebSocketResolver;
import org.xserver.component.core.interfaces.InterfaceMeta;
import org.xserver.component.spring.SpringUtil;

@Component
public class WebSocketResolverManager {
	private Map<String, Class<?>> webSocketResolverInfos = new HashMap<String, Class<?>>(
			100);
	private Map<String, Method> webSocketMethodResolverInfos = new HashMap<String, Method>(
			100);

	public Map<String, Class<?>> getWebSocketResolverInfos() {
		return webSocketResolverInfos;
	}

	public void setWebSocketResolverInfos(
			Map<String, Class<?>> webSocketResolverInfos) {
		this.webSocketResolverInfos = webSocketResolverInfos;
	}

	

	public Map<String, Method> getWebSocketMethodResolverInfos() {
		return webSocketMethodResolverInfos;
	}

	public void setWebSocketMethodResolverInfos(
			Map<String, Method> webSocketMethodResolverInfos) {
		this.webSocketMethodResolverInfos = webSocketMethodResolverInfos;
	}

	/**
	 * Factory method get instance
	 * 
	 * @param attachment
	 * @return
	 */
	public Class getInstance(String topic) {
		ApplicationContext ctx = SpringUtil.getApplicationContext();
		if(webSocketResolverInfos.keySet().size()==0){
			Map<String, Object> objects =  ctx.getBeansWithAnnotation(WebSocketResolver.class);
			 if (null != objects) {
					for (Entry<String, Object> entry : objects.entrySet()) {
						WebSocketResolver classPathAnnotation = entry.getValue().getClass()
								.getAnnotation(WebSocketResolver.class);
						String classPath = classPathAnnotation.value();
						webSocketResolverInfos.put(classPath, entry.getValue().getClass());
						for (Method method : ReflectionUtil.getMethodsWithAnnotation(entry
								.getValue().getClass(), WebSocketResolver.class)) {
							WebSocketResolver methodPathAnnotation = method.getAnnotation(WebSocketResolver.class);
							String methodPath = methodPathAnnotation.value();
							webSocketMethodResolverInfos.put(methodPath, method);

						}
					}
					}
	            }
		return  webSocketResolverInfos.get(topic);
	}
	/**
	 * Factory method get instance
	 * 
	 * @param attachment
	 * @return
	 */
	public Method getMethod(String type) {
		ApplicationContext ctx = SpringUtil.getApplicationContext();
		if(webSocketMethodResolverInfos.keySet().size()==0){
			Map<String, Object> objects =  ctx.getBeansWithAnnotation(WebSocketResolver.class);
			 if (null != objects) {
					for (Entry<String, Object> entry : objects.entrySet()) {
						WebSocketResolver classPathAnnotation = entry.getValue().getClass()
								.getAnnotation(WebSocketResolver.class);
						String classPath = classPathAnnotation.value();
						webSocketResolverInfos.put(classPath, entry.getValue().getClass());
						for (Method method : ReflectionUtil.getMethodsWithAnnotation(entry
								.getValue().getClass(), WebSocketResolver.class)) {
							WebSocketResolver methodPathAnnotation = method.getAnnotation(WebSocketResolver.class);
							String methodPath = methodPathAnnotation.value();
							webSocketMethodResolverInfos.put(methodPath, method);

						}
					}
					}
	            }
		return  webSocketMethodResolverInfos.get(type);
	}
}
